import * as d3 from 'd3';
import { XAxis, YAxis } from '../../typeSC';
import { monthsShort } from './constant';
import Utils from './Utils';

export default class AxisUtils {
    static setTicks(axis: any, scaler: any, chartElement: XAxis | YAxis) {
        let ticks = scaler.ticks();

        if (typeof chartElement.ticks === 'string') {
            if (chartElement.ticks === 'every_month') ticks = scaler.ticks(d3.utcMonth.every(1));
        }

        else if (typeof chartElement.ticks === 'number') {
            ticks = scaler.ticks(chartElement.ticks);
        }

        if (chartElement.last_value) ticks.push(scaler.domain()[1]);

        axis.tickValues(ticks);
    }

    static setTickFormatter(axis: any, chartElement: XAxis | YAxis) {
        axis.tickFormat((d: any, i: any) => {
            if (d instanceof Date && chartElement.date_format === 'month_number_short') {
                return Utils.formatDate(d.getDate()) + '.' + Utils.formatDate((d.getMonth() + 1)) + '.';
            } else if (d instanceof Date && chartElement.date_format === 'month_string_short') {
                return monthsShort[d.getMonth()];
            }

            return d;
        })
        
    }
}